package com.ars.common.servlet.admin;

import com.ars.common.dao.Adduser;
import com.ars.common.beans.User.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addUserServlet")
public class addUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        user.setUserID(request.getParameter("UserID"));
        user.setName(request.getParameter("Name"));
        user.setPassword(request.getParameter("Password"));
        user.setSex(request.getParameter("Sex").equals("男")?"1":"0");
        user.setMailbox(request.getParameter("Mailbox"));
        user.setPhone(request.getParameter("Phone"));

        System.out.println("addUserServlet-test");
//        System.out.println(user.toString());

        Adduser ad = new Adduser();
        try {
            ad.add(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        String url = request.getContextPath() + "/UserInfo";
        response.sendRedirect(url);
    }
}
